//
//  LZTabBarController.m
//  LZHundredJie
//
//  Created by apple on 16/3/27.
//  Copyright © 2016年 m14a.cn. All rights reserved.
//

#import "LZTabBarController.h"
#import "LZNavigationController.h"
#import "LZEssenceViewController.h"
#import "LZFriendTrendViewController.h"
#import "LZMeViewController.h"
#import "LZNewViewController.h"
#import "LZTabBar.h"

@implementation LZTabBarController

// 当类第一次使用的时候,或者子类第一次使用的时候,可以调用多次
+ (void)initialize
{
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = [UIColor redColor];
    
    // 通过appearance统一设置所有UITabBarItem的文字属性
    // 后面带有UI_APPEARANCE_SELECTOR的方法,都可以通过appearance对象来统一设置
    UITabBarItem *item = [UITabBarItem appearance];
    
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    
    // 通过normal状态设置字体大小
    // 字体大小 跟 normal
    NSMutableDictionary *attrnor = [NSMutableDictionary dictionary];
    attrnor[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    [item setTitleTextAttributes:attrnor forState:UIControlStateNormal];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 1.添加子控制器
    [self addChildVC];
    // 2.自定义tabBar
    [self setupTabBar];
}

#pragma mark - 自定义tabBar
- (void)setupTabBar
{
    LZTabBar *tabBar = [[LZTabBar alloc] init];
    // KVC修改,更换tabBar,替换系统的tabBar KVC:设置readonly属性
    [self setValue:tabBar forKeyPath:@"tabBar"];
    /*
     1.查找有没有set方法
     2.查找有没有tabBar
     3.查找有没有_tabBar
     */
    // 设置tabBar背景图片
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tabbar-light"]];
}

// 1.添加子控制器
- (void)addChildVC
{
    
    [self addChildVc:[[LZEssenceViewController alloc] init] image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon" title:@"精华"];
    
    [self addChildVc:[[LZNewViewController alloc] init] image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon" title:@"新帖"];
    
    [self addChildVc:[[LZFriendTrendViewController alloc] init]  image:@"tabBar_friendTrends_icon"selectedImage:@"tabBar_friendTrends_click_icon" title:@"关注"];
    
    UIStoryboard *meStoryBoard = [UIStoryboard storyboardWithName:@"LZMeViewController" bundle:nil];
    LZMeViewController *meVC = [meStoryBoard instantiateInitialViewController];
    
    [self addChildVc:meVC image:@"tabBar_me_icon"selectedImage:@"tabBar_me_click_icon" title:@"我" ];
    
}

/**
 *  添加一个子控制器
 *
 *  @param childVc       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */
- (void)addChildVc:(UIViewController *)childVc image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title
{
    childVc.title = title;
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    childVc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    
    LZNavigationController *nav = [[LZNavigationController alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
}

@end
